package com.love.cloud.common.security.component;

import com.love.cloud.admin.api.entity.SysDept;
import com.love.cloud.common.mybatis.config.MyMetaObjectHandler;
import com.love.cloud.common.security.service.MyUser;
import com.love.cloud.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * mybatis-plus的自动填充执行器
 * @author hongdongqu
 * @since 2020-10-25
 */
@Slf4j
@Component
public class SecurityMetaObjectHandler extends MyMetaObjectHandler {
    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        super.insertFill(metaObject);
        boolean hasCreator=metaObject.hasSetter("creator");
        boolean hasCreatorId=metaObject.hasSetter("creatorId");
        boolean hasCreatorDeptId=metaObject.hasSetter("creatorDeptId");
        boolean hasCreatorDept=metaObject.hasSetter("creatorDeptName");
        boolean hasCreatorDeptPath=metaObject.hasSetter("creatorDeptPath");
        MyUser user = SecurityUtils.getUser();
        //创建人民
        if(hasCreator) {
            Object val=getFieldValByName("creator",metaObject);
            if(val==null){
                if(user!=null&&user.getUserInfo()!=null&&user.getUserInfo().getSysUser()!=null){
                    this.strictInsertFill(metaObject, "creator", String.class, user.getUserInfo().getSysUser().getName());
                }
            }
        }
        //创建人id
        if(hasCreatorId) {
            Object val=getFieldValByName("creatorId",metaObject);
            if(val==null){
                if(user!=null){
                    this.strictInsertFill(metaObject, "creatorId", String.class, String.valueOf(user.getId()));
                }
            }
        }
        //创建人部门id
        if(hasCreatorDeptId) {
            Object val=getFieldValByName("creatorDeptId",metaObject);
            if(val==null){
                if(user!=null&&user.getDeptList()!=null&&user.getDeptList().size()>0){
                    this.strictInsertFill(metaObject, "creatorDeptId", String.class, String.valueOf(this.getSysDept(user.getDeptList()).getId()));
                }
            }
        }
        //创建部门名称
        if(hasCreatorDept) {
            Object val=getFieldValByName("creatorDeptName",metaObject);
            if(val==null){
                if(user!=null&&user.getDeptList()!=null&&user.getDeptList().size()>0){
                    this.strictInsertFill(metaObject, "creatorDeptName", String.class, this.getSysDept(user.getDeptList()).getName());
                }
            }
        }
        //创建部门路径
        if(hasCreatorDeptPath) {
            Object val=getFieldValByName("creatorDeptPath",metaObject);
            if(val==null){
                if(user!=null&&user.getDeptList()!=null&&user.getDeptList().size()>0){
                    this.strictInsertFill(metaObject, "creatorDeptPath", String.class, this.getSysDept(user.getDeptList()).getPath());
                }
            }
        }


    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        super.updateFill(metaObject);
    }

    /**
     *
     * @return
     */
    private SysDept getSysDept(List<SysDept> deptList){
        return deptList.get(0);
    }
}
